
$(function () {
    //变量的抽取:
     var memberPointDataGrid=$("#memberPointDataGrid");  //employeeDataGrid
     var memberPointListDataGrid=$('#memberPointListDataGrid');
     var giftdatagrid = $("#giftdatagrid");
     var ff=$("#ff");


    //datagrid
    $('#memberPointDataGrid').datagrid({
        url: 'memberPoint/list',
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        ctrlSelect:true,
        striped:true,
        onClickRow:onClickRow,
        columns: [[
           {field: 'member', title: '姓名', width: '21%',align:  'center',formatter: function(value,row,index){
                     return value.realName;
               }
           },
           {field: 'memWallet', title: '余额', width: '21%',align:  'center'},
            {field: 'userLevel', title: '卡等', width: '21%',align: 'center'},
            {field: 'payHistory', title: '累积消费', width: '21%',align:  'center'},
            {field: 'point', title: '积分', width: '21%',align:  'center'},
            {field: 'userGroup', title: '分组', width: '21%',align:  'center'},

            {field: 'memCard', title: '卡号', width: '21%',align:  'center'},

            {field: 'userPhone', title: '手机号', width: '21%',align:  'center'},
        ]]
    });

   /* $("#memberPointDataGrid").datagrid('hideColumn', 'userGroup');*/

    $('#memberPointListDataGrid').datagrid({
        url: 'memberPoint/pointlist',
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        ctrlSelect:true,
        striped:true,
        columns: [[
            {field: 'memberCard', title: '会员卡号', width: '14%',align:  'center'},
            {field: 'memberName', title: '会员姓名', width: '14%',align:  'center'},
            {field: 'memberLevel', title: '会员姓名', width: '14%',align:  'center'},
            {field: 'inputUser', title: '操作人', width: '15%',align:  'center'},
            {field: 'changeType', title: '操作类型', width: '15%',align: 'center',formatter:function (v,r) {
                    if (v){
                        return "增加积分";
                    }else {
                        return "扣除积分";
                    }
                }},
            {field: 'changeNum', title: '变动数额', width: '15%',align:  'center'},
            {field: 'changeDate', title: '操作时间', width: '15%',align:  'center'},
        ]]
    });
   /* 积分兑换礼品*/
    /*giftdatagrid.datagrid({
        url: 'gift/list',
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        ctrlSelect:true,
        striped:true,
        columns: [[
            {field: 'giftName', title: '礼品名', width: '14%',align:  'center'},
            {field: 'pointPay', title: '所需积分', width: '14%',align:  'center'},
            {field: 'giftImage', title: 礼品图, width: '14%',align:  'center'},
            {field: 'store', title: '礼品库存', width: '15%',align:  'center'},
        ]]
    });*/


    function onClickRow() {
        var row = $("#memberPointDataGrid").datagrid('getSelected');

        $("#memId").attr('value',row.id);
        var id = row.id;
        var username = row.member.realName
        $('#memberPointListDataGrid').datagrid('load',{"id":id});
        $("#userLevel").html("");
        $("#userLevel").append(row.userLevel);

        $("#userPoint").html("");
        $("#userPoint").append("积分："+row.point);

        $("#userName").html("");
        $("#userName").append(username);

        $("#memlevel").html("");
        $("#memlevel").append(row.userLevel);

        $("#CardNo").html("");
        $("#CardNo").append(row.memCard);

          $("#memwallet").html("");
        $("#memwallet").append(row.memWallet);


        $("#memgroup").html("");
        $("#memgroup").append(row.userGroup);

        $("#memlevel").html("");
        $("#memlevel").append(row.userLevel);


        $("#mempoint").html("");
        $("#mempoint").append(row.point);


    }
    //按钮的事件
    var cmdObj={
        'sure':function () {

            ff.form('submit', {
                url:'memberPoint/changepoint',
                onSubmit: function(){
                },
                success:function(data){
                    // {"success":true,"msg":"操作成功"}==>json字符串
                    var $data = $.parseJSON(data);
                    //弹框提示和刷新
                    if($data.success){
                        $.messager.alert('操作提示',"修改成功！！",'info');
                        $('#ff').form('clear');
                        $('#memberPointDataGrid').datagrid('reload');

                    }else{
                        $.messager.alert('错误提示',$data.msg,'error');

                    }


                }
            });

        },
        'exchange':function () {
            var rowGift = $("#giftdatagrid").datagrid("getSelected");
            var rowMem = $("#memberPointDataGrid").datagrid("getSelected");
            alert(rowGift.pointPay);
            if (rowMem){
                if (rowGift){
                     $.ajax({
                        type: "POST",
                        url: "gift/getGift",
                        data: {"memId":rowMem.id,"giftId":rowGift.id,"rowGift.pointPay":pointPay},
                        dataType: 'json',
                        success: function (data) {
                            if (data.success) {
                                $.messager.alert('提示', "操作成功！", 'info');
                                $("#giftdatagrid").datagrid('reload');
                                $("#memberPointDataGrid").datagrid('reload')
                            } else {
                                $.messager.alert('错误提示', data.msg, 'error');
                            }
                        }
                    });
                }else {
                    $.messager.alert('提示', "请选择兑换的商品！", 'info');
                }
            }else {
                $.messager.alert('提示', "请选择兑换的会员！", 'info');

            }
        },
        'turnToZero':function () {
            var id = $("#memId").val();
            if(id){
                $.messager.confirm('确认对话框', '您确定要清空该会员积分吗？', function(r){
                    if (r){
                        $.ajax({
                            type: "POST",
                            url: "memberPoint/zero",
                            data: {"id":id},
                            dataType: 'json',
                            success: function (data) {
                                if (data.success) {
                                    $.messager.alert('提示', "操作成功！", 'info');
                                    $('#memberPointDataGrid').datagrid('reload');
                                    $('#memberPointListDataGrid').datagrid('reload')
                                } else {
                                    $.messager.alert('错误提示', data.msg, 'error');
                                }
                            }
                        });

                    }
                });
            }else {
                $.messager.alert('错误提示',"请先选择会员", 'error');
            }

        },
        'delete':function () {
            //先获取删除的数据
            var row = studentDatagrid.datagrid('getSelected');
            //做一个判断:是否选中
            if (row){
                $.messager.confirm('温馨提示','你确定删除:[<font color="pink">'+row.name+"</font>]吗?",function(r){
                    // 选中:确认的操作:调用后台的删除方法:发送ajax调用
                    if (r){
                        // 发送ajax调用 $.get(url,params,function(d){},type)
                        $.get('student/delete?id='+row.id,function (d) {
                            //d的处理
                          if(d.success){
                              //true:成功
                              $.messager.alert('操作提示',d.msg,'info');
                              //页面刷新
                              cmdObj.refresh();
                          }else{
                              //false:失败:提示
                              $.messager.alert('错误提示',d.msg,'error');
                          }

                        },'json')
                    }
                });
            }else{
                $.messager.alert('温馨提示','请选中需要删除的行!!','warning');
            }
        },
        'refresh':function () {
            //datagrid的重新加载: $("selector").datagrid('funName');
            studentDatagrid.datagrid('reload');
        },
        'submit':function(){
            //提交:表单的提交:
            ff.form('submit', {
                    url:'student/saveOrUpdate',
                onSubmit: function(){
            },
            success:function(data){
             // {"success":true,"msg":"操作成功"}==>json字符串
                var $data = $.parseJSON(data);
                //弹框提示和刷新
                if($data.success){
                    $.messager.alert('操作提示',$data.msg,'info');
                }else{
                    $.messager.alert('错误提示',$data.msg,'error');

                }

                studentDlg.dialog('close');
                //页面刷新
                cmdObj.refresh();
            }
        });
        },
        'cancel':function(){
            //关闭dialog,不清空form,在打开dialog的时候清空
            studentDlg.dialog('close');
        }

    };

    $("a[data-method]").on('click',function () {

        var dataCmd=$(this).data('method');
        cmdObj[dataCmd]();
    });
});